package vn.wallet.app.service.impl;

// Generated Oct 23, 2012 10:05:44 PM by Hibernate Tools 3.4.0.CR1

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import vn.wallet.app.dao.IDBHelperDAO;
import vn.wallet.app.dao.IUserDAO;
import vn.wallet.app.domain.User;
import vn.wallet.app.helper.AppUtils;
import vn.wallet.app.helper.exceptions.TechnicalException;
import vn.wallet.app.service.IUserService;

@Transactional
@Service
public class UserService implements IUserService {

	private static final Log log = LogFactory.getLog(UserService.class);

	@Autowired
	IUserDAO userDAO;

	@Autowired
	IDBHelperDAO dbHelper;

	@Override
	public User getByUsername(String username) throws Exception {
		List<User> lstUser = userDAO.getByName(username);
		if (lstUser != null && lstUser.size() == 1) {
			return lstUser.get(0);
		}
		return null;
	}

	@Override
	public User getById(int id) throws Exception {
		return userDAO.getById(id);
	}

	@Override
	public void delete(User obj) throws Exception {
		userDAO.delete(obj);
	}

	@Override
	public void delete(int id) throws Exception {
		userDAO.delete(id);
	}

	@Override
	public void update(User obj) throws Exception {
		userDAO.update(obj);
	}

	@Override
	public void save(User obj) throws Exception {
		userDAO.save(obj);
	}

	@Override
	public List<User> getAll() throws Exception {
		return userDAO.getAll();
	}

	@Override
	public List<User> getAllByUserId(int userId) throws Exception {
		throw new TechnicalException("Method not supported");
	}

	@Override
	public List<?> findByExample(User obj) throws Exception {
		return userDAO.findByExample(obj);
	}

	@Override
	public User validateUserLogin(String username, String password, String ip) {
		User ret = null;
		try {
			// encrypt the password
			password = AppUtils.encryptPassword(password);
			System.out.println("password : " + password);
			ret = userDAO.validateUserLogin(username, password, ip);
		} catch (Exception ex) {
			log.error("error when validateUserLogin", ex);
			ret = null;
		}
		return ret;
	}

	public IUserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(IUserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public IDBHelperDAO getDbHelper() {
		return dbHelper;
	}

	public void setDbHelper(IDBHelperDAO dbHelper) {
		this.dbHelper = dbHelper;
	}

}
